package com.itheima.controller;

import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@WebServlet("/updateServlet")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1. 设置POST方式请求编码
            request.setCharacterEncoding("UTF-8");

            //2. 使用BeanUtils封装表单数据
            Map<String, String[]> map = request.getParameterMap();
            Brand brand = new Brand();
            BeanUtils.populate(brand, map);  // 会用  SpringMVC替换

            //3. 调用service完成添加
            BrandService brandService = new BrandServiceImpl();
            brandService.update(brand);

            //4. 转发到查询所有Servlet
            request.getRequestDispatcher("/selectAllServlet").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
